function [route,numExpanded] = AStarGrid (input_map, start_coords, dest_coords,input_map1)
cmap = [1 1 1; ...
0 0 0; ...
1 0 0; ...
0 0 1; ...
0 1 0; ...
1 1 0; ...
0.5 0.5 0.5];
colormap(cmap);
mapR=input_map1;
drawMapEveryTime = false;
[nrows, ncols] = size(input_map);
map = zeros(nrows,ncols);
map1 = zeros(nrows,ncols);
map(input_map==0) = 1;
map(input_map==1) = 2;
map1(input_map1==0) = 1;
map1(input_map1==1) = 2;
start_node = sub2ind(size(map), start_coords(1), start_coords(2));
dest_node = sub2ind(size(map), dest_coords(1), dest_coords(2));
map(start_node) = 5;
map(dest_node) = 6;
map1(start_node) = 5;
map1(dest_node) = 6;
parent = zeros(nrows,ncols);
[X, Y] = meshgrid (1:ncols, 1:nrows);xd = dest_coords(2);
yd = dest_coords(1);
H = sqrt((X^2 - xd^2) + (Y^2 - yd^2));
f = Inf(nrows,ncols);
g = Inf(nrows,ncols);
g(start_node) = 0;
f(start_node) = H(start_node);
numExpanded = 0;
b=input_map;
visted_node=[0,0];
while true
map(start_node) = 5;
map(dest_node) = 6;
if (drawMapEveryTime)
b=map;
[p1,p2]=find(b==4);
for i=1:length(p1)
x=p1(i);
y=p2(i);
[min_f, current] = min(f(:));
if ((current == dest_node) || isinf(min_f))break;
end
map(current) = 3;
f(current) = Inf;
numExpanded=numExpanded+1;
[i, j] = ind2sub(size(f), current);
action=[-1 0; 1 0;0 -1; 0 1;
1,1; 1,-1;-1,1;-1,-1];
for a=1:length(action(:,1))
expand=[i,j]+action(a,:);
expand1=expand(1,1);
expand2=expand(1,2);
if ( expand1>=1 && expand1<=nrows && expand2>=1 && expand2<=ncols && map(expand1,expand2)~=2 && map(expand1,expand2)~=3 && map(expand1,expand2)~=5)
if checkIfinObstacle(map==2,[i,j],expand)
continue;
end
if ( g(expand1,expand2)> g(i,j)+norm(action(a,:)) )
g(expand1,expand2)= g(i,j)+norm(action(a,:))+mapR(expand1,expand2);
f(expand1,expand2)= g(expand1,expand2)+H(expand1,expand2);
parent(expand1,expand2)=current;
map(expand1,expand2)=4;
end
end
end
end
if (isinf(f(dest_node)))
route = [];
else
route = [dest_node];
while (parent(route(1)) ~= 0)
route = [parent(route(1)), route];
end
for k = 2:length(route) - 1
map(route(k)) = 7;
pause(0.1);
b=map;
end
end
end
function[g,f,parent,map,map0]=getAction(i,j,current,nrows,ncols,g,f,H,parent,map,map0) 
action=getDirection(RA);
for a=1:length(action(:,1))
expand=[i,j]+action(a,:);
expand1=expand(1,1);
expand2=expand(1,2);
if ( expand1>=1 && expand1<=nrows && expand2>=1 && expand2<=ncols && map(expand1,expand2)~=2 && map(expand1,expand2)~=3 && map(expand1,expand2)~=5)
if checkIfinObstacle(map==2,[i,j],expand)
continue;
end
if ( g(expand1,expand2)> g(i,j)+norm(action(a,:)) )
g(expand1,expand2)= g(i,j)+norm(action(a,:));
f(expand1,expand2)= g(expand1,expand2)+H(expand1,expand2);
parent(expand1,expand2)=current;
map(expand1,expand2)=4;
map0(expand1,expand2)=4;
end
end
end
end